﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PingPong
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Ball pall;
        MinuPad playerPad;
        AIPad aiPad;

        //Screen parameters
        public int screenWidth;
        public int screenHeight;
        public Game1()
        {

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            screenWidth = GraphicsDevice.Viewport.Width;
            screenHeight = GraphicsDevice.Viewport.Height;
            // TODO: Add your initialization logic here

            pall = new Ball(Content.Load<Texture2D>("Ball"), new Vector2(50, 50),
                 Content.Load<Texture2D>("Ball").Width, Content.Load<Texture2D>("Ball").Height, screenHeight, screenWidth);
            aiPad = new AIPad(Content.Load<Texture2D>("AIPad"), new Vector2(screenWidth / 2, 0), 100, 100, screenHeight, screenWidth);
            playerPad = new MinuPad(Content.Load<Texture2D>("MyPad"), new Vector2((screenWidth/2-75),(screenHeight-25)),
                Content.Load<Texture2D>("MyPad").Width, Content.Load<Texture2D>("MyPad").Height, screenWidth, screenHeight);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }
        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            playerPad.Update();
            if (playerPad.myPadRectangle.Intersects(pall.ballRectangle))
                pall.ballSpeedY =4;
            if (aiPad.AIRectangle.Intersects(pall.ballRectangle))
            {
                pall.ballSpeedY = -4;
                
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))          
                this.Exit();

             playerPad.Update();
             aiPad.Update((int)pall.ballPosition.X);
             pall.Update();

             


        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            pall.Draw(spriteBatch);
            playerPad.Draw(spriteBatch);
            aiPad.Draw(spriteBatch);
            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}
